﻿using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;

namespace Gqq.CodeFirst.Palace
{
    public class UnicornsContext : DbContext
    {
        // 这种写法，必须要在配置文件中有字符串，否则，就会抛异常。
        public UnicornsContext()
            : base("name=UnicornsDatabase")
        {
            //            this.Configuration.ProxyCreationEnabled = false;
        }

        public UnicornsContext(string connString)
            : base("name=" + connString)
        {

        }

        public UnicornsContext(DbConnection existingConnection)
            : base(existingConnection, true)
        {

        }

        public DbSet<Unicorn> Unicorns { get; set; }
        public DbSet<Princess> Princesses { get; set; }
        public DbSet<LadyInWaiting> LadiesInWaiting { get; set; }
        public DbSet<Castle> Castles { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

            Configuration.LazyLoadingEnabled = false;
//            Configuration.ProxyCreationEnabled = false;
            //base.OnModelCreating(modelBuilder);

            // Get the ObjectContext related to this DbContext
            //            var objectContext = (this as IObjectContextAdapter).ObjectContext;

            // Sets the command timeout for all the commands
            //            objectContext.CommandTimeout = 120;

            // Turn off lazy loading for all entities
            //Configuration.LazyLoadingEnabled = false;
        }
    }

}
